﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using AttributeRouting;
using AttributeRouting.Web;
using AttributeRouting.Web.Http;
using MvcWinesSample.DAL;
using MvcWinesSample.DAL.InMemory;
using MvcWinesSample.DAL.Models;
using MvcWinesSample.ViewModels;

namespace MvcWinesSample.Controllers
{
    //[AllowCrossSiteJsonAttribute]
    [RoutePrefix("api/v1/wines")]
    public class WinesController : AppControllerBase
    {
        //TODO:Create a business controller
        #region datasource
        private static readonly IRepository<Wine> Repository = new MemoryRepository<Wine>(); //TODO: inject

        static WinesController()
        {
            for (int i = 1; i <= 4; i++)
                Repository.Save(CreateWine(i));
        }

        private static Wine CreateWine(int id)
        {
            return new Wine()
            {
                Id = id,
                Country = string.Format("country wine {0}", id),
                Description = string.Format("description wine {0}", id),
                Grapes = string.Format("grapes wine {0}", id),
                Name = string.Format("Name wine {0}", id),
                Picture = string.Format("picture{0}.jpg", id),
                Region = string.Format("region wine {0}", id),
                Year = "1999"
            };
        }
        
        #endregion

        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
        }

        //
        // GET: /api/wines/
        [AttributeRouting.Web.Mvc.GET("")]
        public JsonResult All()
        {
            JsonResult jsonResult = Json(WineDetails.From(Repository.FindAll().ToList()), JsonRequestBehavior.AllowGet);
            return jsonResult;
        }

        //
        // GET: /api/wines/{id}
        [AttributeRouting.Web.Mvc.GET("{id}")]
        public JsonResult Read(int id)
        {
            var result = Json(Repository.Get(id)) ;

            return result;
        }

        //
        // POST: /api/wines/
        [AttributeRouting.Web.Mvc.POST("")]
        public JsonResult Create(WineDetails item)
        {
            var wine = Repository.Save((Wine)item);

            return Json((WineDetails)wine);
        }

        //
        // PUT: /api/wines/{id}
        [AttributeRouting.Web.Mvc.PUT("{id}")]
        public JsonResult Update(int id,WineDetails item)
        {
            var wine = Repository.Save((Wine)item);

            return Json((WineDetails)wine);
        }
        //
        // DELETE: /api/wines/{id}
        [AttributeRouting.Web.Mvc.DELETE("{id}")]
        public JsonResult Delete(int id)
        {
            Wine data;
            if((data=Repository.Get(id))!=null)
            {
                Repository.Delete(data);
            }
            return Json(new {});
        }

    }
}
